package ke07;

import java.io.IOException;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

public class UrlTest extends Authenticator{
	
	protected PasswordAuthentication getPasswordAuthentication() {
		String hostname = getRequestingHost();
		System.out.println(hostname);
		String username = "name";
		String password = "password";
		return new PasswordAuthentication(username, password.toCharArray());
	}

	public static void main(String[] args) throws IOException {

		URL url = new URL(
				"http://www.pst.ifi.lmu.de/Personen/team/raed/javakurs2/java-fortgeschritten");

		@SuppressWarnings("unused")
		URL url2 = new URL("http", "www.pst.ifi.lmu.de",
				"/Personen/team/raed/javakurs2/java-fortgeschritten");

		URL domainUrl = new URL("http://www.pst.ifi.lmu.de");
		URL url3 = new URL(domainUrl,
				"Personen/team/raed/javakurs2/java-fortgeschritten");

//		System.out.println(url3.getProtocol());
//		System.out.println(url3.getHost());
//		System.out.println(url3.getFile());

//		URLConnection con = url.openConnection();
//
//		System.out.println("Date            : " + new Date(con.getDate()));
//		System.out.println("Last Modified   : "
//				+ new Date(con.getLastModified()));
//		System.out.println("Content encoding: " + con.getContentEncoding());
//		System.out.println("Content type    : " + con.getContentType());
		
		Authenticator.setDefault(new UrlTest());
		
		InetAddress inet = InetAddress.getByName( "www.pst.ifi.lmu.de" );
		System.out.println(inet.getCanonicalHostName()); 
		System.out.println(inet.getHostAddress());      
		System.out.println(inet.getHostName());

		inet = InetAddress.getByName("129.187.228.173");
		System.out.println(inet.getHostName());
//		
//		System.out.println(InetAddress.getByName("129.187.228.173").isReachable(2000)) ;      // true
//		System.out.println(InetAddress.getByName("100.100.100.100").isReachable(2000)) ;      // false
//		
//		System.out.println("Host Name/Adresse: " + InetAddress.getLocalHost());
	}
}
